package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.Answer;
import com.tanhua.domain.mongo.SoulQuestionOption;
import com.tanhua.domain.mongo.SoulReport;
import com.tanhua.domain.mongo.SoulResult;

import com.tanhua.domain.vo.DimensionVO;
import com.tanhua.domain.vo.ReportVO;
import com.tanhua.domain.vo.SimilarUserVO;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class SoulReportApiImpl implements SoulReportApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public ReportVO findReport(String reportId, Long userId) {

        ReportVO reportVO = new ReportVO();

        //查询用户的答题结果
        Query query = new Query();
        query.addCriteria(Criteria.where("reportId").is(new ObjectId(reportId))
                .and("userId").is(userId));
        SoulResult soulResult = mongoTemplate.findOne(query, SoulResult.class);
        if (soulResult == null) {
            soulResult = new SoulResult();
        }

        //根据每个选项的得分计算四个维度项的百分比
        List<DimensionVO> dimensions = calDimension(soulResult);
        reportVO.setDimensions(dimensions);

        //similarYou, 根据同一个报告id查询相同的用户
        List<SimilarUserVO> similarYou = new ArrayList<>();

        query = new Query();
        query.addCriteria(Criteria.where("reportId").is(new ObjectId(reportId)));
        List<SoulResult> soulResults = mongoTemplate.find(query, SoulResult.class);

        if (soulResults.size() > 1) {
            for (SoulResult result : soulResults) {
                //排除自己
                if (userId.equals(result.getUserId())) {
                    continue;
                }
                SimilarUserVO user = new SimilarUserVO();
                user.setId(result.getUserId());
                similarYou.add(user);
            }
            reportVO.setSimilarYou(similarYou);
        }

        //查询报告封面
        SoulReport report = mongoTemplate.findOne(Query.query(Criteria.where("id").is(reportId)), SoulReport.class);
        reportVO.setCover(report.getCover());
        reportVO.setConclusion(report.getConclusion());
        return reportVO;
    }


    //根据每个选项的得分来计算纬度项的百分比
    private List<DimensionVO> calDimension(SoulResult soulResult) {
        List<DimensionVO> dimensions = new ArrayList<>();
        //表示外向
        DimensionVO dimension1 = new DimensionVO();
        int count1 = 0;
        //表示判断
        DimensionVO dimension2 = new DimensionVO();
        int count2 = 0;
        //表示抽象
        DimensionVO dimension3 = new DimensionVO();
        int count3 = 0;
        //表示理性
        DimensionVO dimension4 = new DimensionVO();
        int count4 = 0;

        //查询每个用户某个问卷的的答题结果
        List<Answer> answers = soulResult.getAnswers();
        for (Answer answer : answers) {
            /**
             * 根据每一个选项的确定一个纬度项和值
             *  Q:你喜欢什么颜色
             *   option：
             *     红色： 7、6分 => (外向 + 1)
             *     蓝色： 5分 =>   (判断 + 1)
             *     绿色： 4、3 =>   (抽象 + 1)
             *     紫色： 1、2分 => (理性 + 1)
             */
            SoulQuestionOption option = mongoTemplate.findOne(new Query(Criteria.where("id").is(new ObjectId(answer.getOptionId()))), SoulQuestionOption.class);
            //1-7
            if (option.getScore() < 3) {
                count1++;
            } else if (option.getScore() < 5) {
                count2++;
            } else if (option.getScore() < 7) {
                count3++;
            } else {
                count4++;
            }
        }
        //根据分值设置维度项值
        {
            dimension1.setKey("外向");
            dimension1.setValue(count1);
            dimension2.setKey("判断");
            dimension2.setValue(count2);
            dimension3.setKey("抽象");
            dimension3.setValue(count3);
            dimension4.setKey("理性");
            dimension4.setValue(count4);

            dimensions.add(dimension1);
            dimensions.add(dimension2);
            dimensions.add(dimension3);
            dimensions.add(dimension4);
        }
        return dimensions;
    }
}
